<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" :model="form" label-width="100px" label-position="left" :inline="true">
        <el-form-item label="节目单名称" prop="strUsername">
          <el-input v-model="form.strUsername" placeholder="请输入节目单名称" />
        </el-form-item>
        <el-form-item label="节目单类型" prop="iUrgent">
          <el-select v-model="form.iUrgent" placeholder="请选择">
            <el-option
              v-for="item in programType"
              ref="refHandle"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所在机构" prop="strOrgnum">
          <el-cascader
            v-model="strOrgnum"
            :show-all-levels="false"
            :options="optionslist"
            :props="{ expandTrigger: 'hover',checkStrictly: true, value:'strOrgnum',label:'strOrgname',children: 'yhlist' }"
          />
        </el-form-item>
        <el-button type="primary" @click="handleSearch()">搜索</el-button>
        <el-button @click="$refs['form'].resetFields()">重置</el-button>
        <el-button type="primary">
          <router-link :to="{ path: 'addProgramme' }">新增节目单</router-link>
        </el-button>
        <!--<el-button type="danger" @click="handleDeleteAll(numArr)" :disabled="this.numArr.length === 0">批量删除
        </el-button>-->
      </el-form>
    </div>
    <el-table
      :data="listData"
      style="width: 100%"
      border
      @selection-change="selsChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        prop="ID"
        :index="indexMethod"
        label="序号"
        width="50"
        align="center"
      />
      <!--<el-table-column prop="strProgramnum" label="节目单编号" align="center"></el-table-column>-->
      <el-table-column prop="strUsername" label="节目单名称" align="center" />
      <el-table-column prop="strCycle" label="播放周期" align="center" />
      <el-table-column prop="strStartdate" label="生效日期" align="center" />
      <el-table-column prop="strEnddate" label="截至日期" align="center" />
      <el-table-column prop="iUrgent" label="节目单类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.iUrgent === 1 ? '紧急' : '普通' }}
        </template>
      </el-table-column>
      <el-table-column prop="countnum" label="素材数量" align="center" />
      <el-table-column prop="strOrgname" label="所在机构" align="center"/>
      <el-table-column prop="strOperatorname" label="创建者" align="center" />
      <el-table-column prop="strCreatetime" label="创建时间" align="center" />
      <el-table-column label="操作" align="center" width="255">
        <template slot-scope="{row,$index}">
          <template v-if="iOrgsysnum === -1||row.iOrgsysnum === iOrgsysnum">
            <el-button size="mini" type="primary" @click="editProgramme(row,$index)">
              修改
              <!--<router-link :to="{ path: 'editProgramme' }">修改</router-link>-->
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row,$index)">删除</el-button>
            <el-button size="mini" type="success" @click="ShowDetail(row,$index)">详情</el-button>
          </template>
          <template v-else>
            <el-button size="mini" type="success" @click="ShowDetail(row,$index),dialogTableVisible = true">
              详情
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-show="total>0"
      :total="total"
      :page.sync="form.pagenum"
      :limit.sync="limit"
      :layout="layout"
      @pagination="fetchList(form)"
    />
    <!--详情节目单弹窗-->
    <el-dialog title="节目单详情" :visible.sync="dialogTableVisible" :close-on-click-modal="false">
      <h3>基本信息</h3>
      <el-table :data="gridData" border>
        <el-table-column type="index" prop="id" align="center" label="序号" width="55" />
        <el-table-column prop="strProgramnum" align="center" label="节目单编号" width="160" />
        <el-table-column prop="strUsername" align="center" label="节目单名称" width="160" />
        <el-table-column prop="strCycle" align="center" label="播放周期" width="160" />
        <el-table-column prop="strEnddate" align="center" label="有效期" width="160" />
        <el-table-column prop="iUrgent" label="节目单类型" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.iUrgent === 1 ? '紧急' : '普通' }}
          </template>
        </el-table-column>
        <el-table-column prop="iFrequency" align="center" label="素材数量" width="160" />
      </el-table>
      <h3>素材数据</h3>
      <el-table :data="gridDatas" border>
        <el-table-column type="index" prop="ID" label="序号" align="center" width="55" />
        <el-table-column prop="strMaterialname" label="标题" align="center" />
        <el-table-column prop="typeName" label="类型" align="center" />
        <el-table-column prop="strOrgname" label="所属机构" align="center" />
        <el-table-column prop="strDownload" label="下载地址" align="center" />
        <el-table-column prop="iPlaytime" label="播放次数" align="center" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProgramList,
  getOrglist,
  deleteProgram,
  detailPrograms,
  detailsucai,
  udpRelease,
  deleteAllProgramList,
  getOrglists
} from '../../api/addTag'
import Pagination from '@/components/Pagination'
import { mapGetters } from 'vuex'

export default {
  components: { Pagination },
  data() {
    return {
      total: 0,
      limit: 5,
      pagenum: 1,
      currentpage: 1,
      layout: 'total, prev, pager, next, jumper',

      dialogTableVisible: false,
      // 搜索表单
      form: {
        strProgramType: '',
        strProgramnum: '',
        strUsername: '',
        strOrgname: '',
        strOrgnum:'',
        value: '',
        iUrgent: '',
        pagenum: 1,
        userOrg: this.$store.getters.iOrgsysnum
      },
      // 节目单类型
      programType: [
        {
          value: '1',
          label: '紧急'
        },
        {
          value: '0',
          label: '普通'
        }
      ],
      strOrgnum:[],
      orgList: [],
      optionslist: [], // 所属机构有权限
      listData: [],
      gridData: [], // 节目单列表
      gridDatas: [], // 从素材列表
      numArr: [], // 批量删除
      userOrg: '', // 登录时用户所属机构
      iOrgnum: this.$store.getters.iOrgsysnum,
    }
  },
  watch: {
    handlerValue() {
      if (this.$refs.refHandle) {
        this.$refs.refHandle.dropDownVisible = false // 监听值发生变化就关闭它
      }
    }
  },
  computed: {
    ...mapGetters(['iOrgsysnum'])
  },
  created() {
    this.fetchList()
    this.getOrglistsq()
  },
  mounted() {
    // 点击文本就让它自动点击前面的input就可以触发选择。但是因组件阻止了冒泡，暂时想不到好方法来触发。
    // 这种比较耗性能，暂时想不到其他的，能实现效果了。
    setInterval(function() {
      document.querySelectorAll('.el-cascader-node__label').forEach(el => {
        el.onclick = function() {
          if (this.previousElementSibling) this.previousElementSibling.click()
        }
      })
    }, 1000)
  },
  methods: {
    fetchList() {
      // this.userOrg = this.$store.getters.iOrgsysnum
      /*if (this.form.strOrgname) {
        const arr = this.form.strOrgname.splice(-1)
        this.form.strOrgname = arr[0]
      }*/
      if (this.form.strOrgname instanceof Array) {
        this.form.strOrgname = this.form.strOrgname.splice(-1)[0]
      }

      // 获取节目单table数据
      getProgramList(this.form).then(res => {
        this.listData = res.pages
        this.total = res.total
        this.currentpage = res.currentpage
      })
      // 获取机构数据
      getOrglist().then(res => {
        /* this.orgList = res.yhlist*/
        this.orgList = res.data
      })
    },
    // 获取所属机构有权限
    getOrglistsq() {
      getOrglists({ iOrgnum: this.iOrgnum }).then(res => {
        // console.log(res)
        this.optionslist.push(res.data)
        // this.optionslist = res.data
      })
    },
    // 搜索按钮
    handleSearch() {
      /* this.ruleForm.iOrgsysnum = row.iOrgsysnum.toString()
        this.form.strProgramType = this.programType.label*/
      this.fetchList(this.form)
    },

    // 修改节目单
    editProgramme(row, index) {
      detailsucai({ iProgramsysnum: row.iProgramsysnum }).then(res => {
        this.materialData = res.materialInfoList
        this.$router.push({ path: '/programme/editProgramme', query: { rowAll: row, dataAll: this.materialData }})
      })
      // this.$router.push({name:'strUsername',query:{strUsername:row.strUsername}})
    },

    // 发布节目单
    ReleaseProgramrow(row, index) {
      udpRelease({ id: row.id, iRelease: 1 }).then(res => {
        this.fetchList()
      })
    },

    // 详情节目单
    ShowDetail(row, index) {
      // 节目单列表
      detailPrograms({ iProgramsysnum: row.iProgramsysnum }).then(res => {
        this.gridData = []
        this.gridData.push(res.programList)
        this.dialogTableVisible = true
      }).catch(err => {
        console.log(err)
      })
      // 素材列表
      detailsucai({ iProgramsysnum: row.iProgramsysnum }).then(res => {
        this.gridDatas = []
        this.gridDatas = res.materialInfoList
      })
    },

    // 删除单条节目单
    handleDelete(row, index) {
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteProgram({ iOrgsysnum: row.iOrgsysnum, iProgramsysnum: row.iProgramsysnum }).then(res => {
          this.$message({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.fetchList()
        })
      })
    },

    // 触发复选框（批量删除用）
    selsChange(val) {
      this.numArr = val
    },
    // 批量删除节目单
    /* handleDeleteAll(){
        let ids = []
        this.numArr.map(item=>{
          ids.push(item.iProgramsysnum)
        })
        this.numArr = ids
        console.log(this.numArr)
        deleteAllProgramList({ numArr: ids }).then(res=>{
        this.numArr = ids
        this.fetchList({ pagenum: this.pagenum })
        })
      },*/
    /* handleDeleteAll() {
        deleteAllProgramList().then(res => {
          let ids = []
          this.numArr.map(item=>{
            ids.push(item.iProgramsysnum)
          })
          this.numArr = ids
          console.log(this.numArr)
          this.$confirm('确认删除吗？', '提示', {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteAllProgramList({ numArr: ids }).then(res => {
              this.$message({
                title: 'Success',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.numArr = ids
              this.fetchList({ pagenum: this.pagenum })
            })
          }).catch(err => {
            console.error(err)
          })
        })
      },*/

    // 批量删除节目单
    handleDeleteAll() {
      const ids = []
      this.numArr.map(item => {
        ids.push(item.iProgramsysnum)
      })
      this.numArr = ids
      console.log(this.numArr)
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAllProgramList({ numArr: ids }).then(res => {
          this.$message({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.numArr = ids
          this.fetchList()
        })
      }).catch(err => {
        console.error(err)
      })
    },

    // 列表分页序号排序
    indexMethod(index) {
      return (index + 1) + (this.form.pagenum - 1) * this.limit
    }
  }
}
</script>

<style lang="scss" scope>
  .el-cascader-panel .el-radio {
    width: 100%;
    height: 100%;
    z-index: 10;
    position: absolute;
    top: 10px;
    right: 10px;
  }

  .el-cascader-panel .el-radio__input {
    visibility: hidden;
  }

  .el-cascader-panel .el-cascader-node__postfix {
    top: 10px;
  }

  .btnmargin {
    margin: 0;
  }
</style>
